package impl

import (
	"context"
	"github.com/starmotioncf/common/third_game"
	"time"
)

// ！！！！ 这里是common.third_game 依赖方法的实现， 提供给 common.third_game 使用

// CreateToken 创建token
func (s *thirdGameService) CreateToken(ctx context.Context, uid uint64, p third_game.Provider, exp time.Duration) (string, time.Time, error) {
	return s.jwtToken.CreateToken(uid, p.AccountId(), exp)
}

// ValidateToken 校验token
func (s *thirdGameService) ValidateToken(ctx context.Context, tokenStr string, p third_game.Provider) (uid uint64, err error) {
	return s.jwtToken.ValidateToken(tokenStr, p.AccountId())
}

func (s *thirdGameService) GetUserInfo(ctx context.Context, uid uint64, thirdGameId string, bs third_game.Provider) (*third_game.UserInfo, error) {
	userInfo, err := s.userRepo.Get(ctx, uid)
	if err != nil {
		return nil, err
	}
	//rgn := userInfo.Region
	////获取游戏信息
	//gameInfo, err := s.GetGameInfo(thirdGameId, companyCode, platformCode)
	//if err != nil {
	//	return nil, err
	//}
	// todo 应该检查用户的封禁情况
	// ...

	return &third_game.UserInfo{
		Uid:       userInfo.Uid,
		Nickname:  userInfo.Nickname,
		AvatarUrl: userInfo.Avatar,
		Gold:      userInfo.Gold,
		Level:     0,
		DrawWater: 0,
	}, nil
}

func (s *thirdGameService) ChangeGold(ctx context.Context, info third_game.ChangeGold) (lastGold int64, err error) {
	//todo 第一个步骤应该检查订单是否存在
	// .....
	//if info.Gold == 0 {
	//	return 0, errors.New("gold is zero")
	//}
	//ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
	//defer cancel()
	//////操作金币
	//var typ uint8
	//if info.GoldAction == third_game.GoldDeposit {
	//	err = s.AddGold(ctx, repository.Record{
	//		Uid:    info.Uid,
	//		Time:   info.Now,
	//		RType:  constant.GoldType(info.Game.GetDepositRType()),
	//		Number: int64(info.Gold),
	//	})
	//	typ = 2
	//} else {
	//	err = s.CutGold(ctx, repository.Record{
	//		Uid:    info.Uid,
	//		Time:   info.Now,
	//		RType:  constant.GoldType(info.Game.GetConsumeRType()),
	//		Number: int64(info.Gold),
	//	})
	//	typ = 1
	//}
	//if err != nil {
	//	//金币不足，犯有游戏方定义的错误码
	//	if errors.Is(err, repository.ErrGoldNotEnough) {
	//		return 0, third_game.ErrGoldNotEnough
	//	}
	//	return 0, err
	//}
	//pl := event.ThirdGameGoldUpdatePayload{
	//	Uid:     info.Uid,
	//	Game:    info.Game.(*model2.VcThirdGame),
	//	Now:     info.Now,
	//	RoundId: info.RoundId,
	//	OrderId: info.OrderId,
	//	RoomId:  info.RoomId,
	//	WinId:   info.WinId,
	//	Gold:    int64(info.Gold),
	//	Typ:     typ,
	//	Extend:  info.Extend,
	//}
	////传递抽水值去保存
	//if drawWaterConf, ok := s.drawWaterCache.Load(s.getDrawWaterCacheKey(info.Uid, info.Game.GetGameId(), info.Game.GetPlatformCode())); ok && drawWaterConf != nil {
	//	if tw, _ok := drawWaterConf.(*model2.VcThirdGameDrawWater); _ok {
	//		pl.DrawWaterId = tw.Id
	//		if tw.Level > 0 {
	//			pl.DrawWaterVal = tw.Level
	//		} else if tw.DrawWater > 0 {
	//			pl.DrawWaterVal = tw.DrawWater
	//		}
	//	}
	//}
	////发布游戏金币变动事件
	//s.eventBus.PublishAsync(event2.NewEvent(event.ThirdGameGoldUpdateEvent, pl))
	//lastGold = s.GetGold(ctx, info.Uid)
	lastGold = 0
	return
}

func (s *thirdGameService) GetGameInfo(ctx context.Context, gameId string, p third_game.Provider) (third_game.Game, error) {
	//return s.thirdGameRepo.GetByPlatformAndId(context.Background(), Get, gameId)
	return nil, nil
}

// func (s *thirdGameService) TestMatchDrawWater(ctx context.Context, uid uint64, countryId int64, rgn region.Region, gameInfo third_game.Game) *model2.VcThirdGameDrawWater {
// 	return s.matchDrawWater(ctx, uid, countryId, rgn, gameInfo)
// }

// func (s *thirdGameService) matchDrawWater(ctx context.Context, uid uint64, countryId int64, rgn region.Region, gameInfo third_game.Game) *model2.VcThirdGameDrawWater {
// confList, _err := s.thirdGameRepo.GetDrawWaterByPlatform(ctx, rgn.Code(), gameInfo.GetPlatformCode())
// //logs.Debug("matchDrawWater: ", uid, countryId, rgn, gameInfo.GetPlatformCode(), confList, _err)
// if _err != nil {
// 	logs.Error("获取抽水配置失败：", gameInfo.GetPlatformCode(), _err)
// 	return nil
// }
// var currDrawWater *model2.VcThirdGameDrawWater
// if len(confList) > 0 {
// 	currLv := 99 //当前匹配中的优先级
// 	for _, item := range confList {
// 		//匹配游戏
// 		if len(item.GameIds.Data) == 0 || slices.Contains(item.GameIds.Data, gameInfo.GetId()) {
// 			switch item.Category {
// 			case constant.ThirdGameWaterCategoryUid:
// 				if currLv <= 1 {
// 					break
// 				}
// 				if slices.Contains(item.Uids.Data, int64(uid)) {
// 					currDrawWater = item
// 					currLv = 1
// 				}
// 			case constant.ThirdGameWaterCategoryCountry:
// 				if currLv <= 2 {
// 					break
// 				}
// 				if len(item.CountryId.Data) == 0 || slices.Contains(item.CountryId.Data, countryId) {
// 					currDrawWater = item
// 					currLv = 2
// 				}
// 			}
// 			if currLv == 1 {
// 				break
// 			}
// 		}
// 	}
// }
// return currDrawWater
// }
